//
//  GCMTaskHttpRequst.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/18.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMTaskHttpRequst.h"
#import "GCMAboutTaskModel.h"
#import "GCMTaskListModel.h"
#import "GCMLXKHModel.h"

@implementation GCMTaskHttpRequst


+ (NSURLSessionDataTask *)postDataWithreceiveId:(NSString *)receiveId withPage:(NSString *)page Success:(void (^) (id obj , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"receiveId"] = receiveId;
    parameters[@"page"] = page;
    
    return [[CZHAppNetAPIClient sharedClient] POST:RECEIVETASKS_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        ///NSLog(@"1111%@",dict);
        
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict objectForKey:@"data"];
//        NSLog(@"/////%@",reciveListArray);
        for (NSDictionary *dict1 in reciveListArray) {
            
            GCMAboutTaskModel *listModel = [[GCMAboutTaskModel alloc]initWithDictionary:[self getValueFromDictionary:dict1] error:nil];
            listModel.idd = [dict1 valueForKey:@"id"];
               NSLog(@"%@",listModel);
            [ReciveListArray addObject:listModel];
            //NSLog(@"---%@",ReciveListArray);
        }
      
        if (success) {
            
                success(ReciveListArray,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)postTaskListDataWithtaskId:(NSString *)taskId Success:(void (^) (id obj , NSError *))success{
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"taskId"] = taskId;
    
    return [[CZHAppNetAPIClient sharedClient] POST:TASKDETAIL_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
     
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        if (success) {
            success(dict,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}
+ (NSURLSessionDataTask *)postLXKHListDataWithtaskId:(NSString *)taskId WithPage:(NSString *)page Success:(void (^) (id obj , NSError *))success{
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    parameters[@"taskId"] = taskId;
    parameters[@"page"] = page;
    
    return [[CZHAppNetAPIClient sharedClient] POST:TASKCONTACTLIST_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //NSLog(@"+nihao +1+  %@",dict);
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict objectForKey:@"data"];
        for (NSDictionary *dict1 in reciveListArray) {
            GCMLXKHModel *listModel = [[GCMLXKHModel alloc]initWithDictionary:[CZHChangeNullString getValueFromDictionary:dict1] error:nil];
            //   NSLog(@"%@",listModel);
            [ReciveListArray addObject:listModel];
            //NSLog(@"---%@",ReciveListArray);
        }
        if (success) {
            success(ReciveListArray,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}


#pragma mark 将字典里的null 转换为空字符
+ (NSDictionary *)getValueFromDictionary:(NSDictionary *)dic
{
    NSMutableDictionary *valueDic = [[NSMutableDictionary alloc] init];
    NSArray *keyArray = [dic allKeys];
    NSMutableArray *valueS = [[NSMutableArray alloc] init];
    for (int i = 0; i < keyArray.count; i ++) {
        NSString *key = [keyArray objectAtIndex:i];
        NSString *value = [NSString stringWithFormat:@"%@",[dic objectForKey:key]];
        if ([value isEqualToString:@"null"] ||  [value isEqualToString:@"<null>"] || [value isEqualToString:@"(null)"]) {
            value = @"";
        }
        [valueS addObject:value];
        [valueDic setObject:value forKey:key];
    }
    return valueDic;
}

+ (NSURLSessionDataTask *)postTaskListDataWithcourtId:(NSString *)courtId Withremark:(NSString *)remark WithPage:(NSString *)page WithUserName:(NSString *)username Success:(void (^) (id obj , NSError *))success{
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"courtId"] = courtId;
    parameters[@"remark"] = remark;
    parameters[@"page"] = page;
    parameters[@"userName"] = username;

    
    
    return [[CZHAppNetAPIClient sharedClient] POST:TASKLIST_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //NSLog(@"+nihao +1+  %@",dict);
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict objectForKey:@"data"];
        for (NSDictionary *dict1 in reciveListArray) {
            GCMTaskListModel *listModel = [[GCMTaskListModel alloc]initWithDictionary:[CZHChangeNullString getValueFromDictionary:dict1] error:nil];
            listModel.idd = [dict1 valueForKey:@"id"];
            //   NSLog(@"%@",listModel);
            [ReciveListArray addObject:listModel];
            //NSLog(@"---%@",ReciveListArray);
        }
        if (success) {
            success(ReciveListArray,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)postDataWithParmerts:(NSDictionary *)parmerts WithUrl:(NSString *)url Success:(void (^) (id obj , NSError *))success{

    return [[CZHAppNetAPIClient sharedClient] POST:url parameters:parmerts success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSLog(@"*新增接待1%@",responseObject);
        NSString *str=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"*新增接待1%@",dict);
        if (success) {
            success(dict,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
}

@end
